﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using Common.Logging;
using System.IO;
using OpenPop.Pop3;
using OpenPop.Mime;
using System.Net.Mail;
using System.Net;
using Quartz.Impl.Triggers;
using Quartz.Impl;
namespace Demo
{
   public  class Simple2Job : IJob
    {
        private const string FolderName = "QuartzDemo5";
        private const string SubfolderName = "Log";
        private static readonly ILog log = LogManager.GetLogger(typeof(Simple2Job));

        /// <summary>
        /// Called by the <see cref="IScheduler" /> when a
        /// <see cref="ITrigger" /> fires that is associated with the <see cref="IJob" />.
        /// </summary>
        public virtual void Execute(IJobExecutionContext context)
        {
       
            //var strDirPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + FolderName + "\\" + SubfolderName;
            //if (System.IO.Directory.Exists(strDirPath) == false)
            //{
            //    System.IO.Directory.CreateDirectory(strDirPath);
            //}
            //string strLogPath = string.Format("{0}\\QuartzDemo_{1}_{2}.txt", strDirPath, DateTime.Now.Month.ToString(), DateTime.Now.Year.ToString());
            //FileStream file = File.Exists(strLogPath) ? new FileStream(strLogPath, FileMode.Append, FileAccess.Write) : new FileStream(strLogPath, FileMode.CreateNew, FileAccess.Write);
            //var sw = new StreamWriter(file);
            //sw.WriteLine("{0} : NOTE # {1}", DateTime.Now, "Current Time is : {0}", DateTime.Now);
            //sw.Close();
            //file.Close();
               // The client disconnects from the server when being disposed
          
            using (Pop3Client client = new Pop3Client())
            {
                // Connect to the server
                client.Connect("pop.gmail.com", 995, true);

                // Authenticate ourselves towards the server
                client.Authenticate("hoangtuananh091987@gmail.com", "nhanmymydinh");

                // Fetch all the current uids seen
                List<string> uids = client.GetMessageUids();

                // Create a list we can return with all new messages
                List<Message> newMessages = new List<Message>();
                if (uids.Count > 0)
                {
                    //Q add
                    Message unseenMessage = client.GetMessage(5);
                    // FileInfo about the location to save/load message
                    FileInfo file = new FileInfo("someFile.eml");

                    // Save the full message to some file
                  //  unseenMessage.Save(file);

                    // Now load the message again. This could be done at a later point
                    Message loadedMessage = Message.Load(file);
                   // MessagePart body = unseenMessage.FindFirstPlainTextVersion();
                    //string Body = body.GetBodyAsText();
                    //StringBuilder builder = new StringBuilder();
                    //builder.Append(body.GetBodyAsText());
                    //string s4 = builder.ToString();
                    //using (var message = new MailMessage("hoangtuananh091987@gmail.com", "ngominhquan.84@gmail.com"))
                    //{
                       
                    //    message.Subject = "Test";
                    //    message.Body = System.Text.Encoding.UTF8.GetString(body.Body);
                    //    using (SmtpClient client1 = new SmtpClient
                    //    {
                    //        EnableSsl = true,
                    //        Host = "smtp.gmail.com",
                    //        Port = 587,
                    //        Credentials = new NetworkCredential("hoangtuananh091987@gmail.com", "nhanmymydinh")
                    //    })
                    //    {
                    //        client1.Send(message);
                    //    }
                    //}
                
                }
            }
        }
       public void stop_schedule()
       {
           IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
           JobKey j = new JobKey("","");
           TriggerKey t = new TriggerKey("","");
           
           scheduler.DeleteJob(j);
           scheduler.UnscheduleJob(t);
           scheduler.Interrupt(j);
           scheduler.Shutdown(true);
          
       }
    }
}
